package com.wanxi.spring.cloud.team.controller;


import com.wanxi.spring.cloud.commons.api.CommonResult;
import com.wanxi.spring.cloud.team.model.Team;
import com.wanxi.spring.cloud.team.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 蒋帆
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    TeamService teamService;

    @GetMapping("/getTeams")
    public CommonResult getTeams(@RequestParam Map map) {
        return teamService.getTeamList(map);
    }

    @GetMapping("/getTeamByName")
    public CommonResult getTeamByName(@RequestParam String name) {
        return teamService.getTeamByName(name);
    }

    @PostMapping("/addTeam")
    public CommonResult addTeam(@RequestBody Team team) {
        team.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        team.setCrew(0);
        team.setEnable("yes");
        int result = teamService.addTeam(team);
        if (result>0)
            return CommonResult.success(true, "添加成功");
        return CommonResult.failed("添加失败");
    }

    @PostMapping("/updateTeam")
    public CommonResult updateTeam(@RequestBody Team team) {
        return teamService.updateTeam(team);
    }

    @PostMapping("/deleteTeam")
    public CommonResult deleteTeam(@RequestParam Integer teamId) {
        return teamService.deleteTeam(teamId);
    }

}
